﻿using System;
using System.Data.SqlClient;
using BikeBouncer.BusinessLayer.Entities;
using Devv.Core.UniDataTier;

namespace BikeBouncer.BusinessLayer
{
	public partial class DataAccess
	{
		#region Get

		public static SqlDataReader City_Get(Int32 id)
		{
			DataHandler db = new DataHandler();

			db.AddParameter("@CityId", id);

			return (SqlDataReader)db.ExecuteReader("upCity_Get");
		}

		public static SqlDataReader City_Get(String title)
		{
			DataHandler db = new DataHandler();

			db.AddParameter("@Title", title);

			return (SqlDataReader)db.ExecuteReader("upCity_Get");
		}

		public static SqlDataReader City_GetExact(String title, String region, String country)
		{
			DataHandler db = new DataHandler();

			if (!String.IsNullOrEmpty(region))
			{
				db.AddParameter("@ExactTitle", title);
			}

			if (!String.IsNullOrEmpty(region))
			{
				db.AddParameter("@ExactRegion", region);
			}

			db.AddParameter("@Country", country);
			
			return (SqlDataReader)db.ExecuteReader("upCity_Get");
		}

		public static SqlDataReader City_GetByCountry(String country)
		{
			DataHandler db = new DataHandler();

			db.AddParameter("@Country", country);

			return (SqlDataReader)db.ExecuteReader("upCity_Get");
		}

		#endregion

		#region Insert and update

		public static void City_Upsert(City city)
		{
			using (DataHandler db = new DataHandler())
			{
				db.AddParameter("@Title", city.Title, true);
				db.AddParameter("@Region", city.Region, true);
				db.AddParameter("@Country", city.Country, true);
				db.AddParameter("@Active", city.Active);

				if (city.Id > 0)
				{
					db.AddParameter("@City", city.Id);

					db.ExecuteScalar("upCity_Update");
				}
				else
				{
					city.Id = Convert.ToInt32(db.ExecuteScalar("upCity_Insert"));
				}
			}
		}

		#endregion
	}
}